import service.AccountService;
import service.TransactionService;

import java.util.Scanner;

public class ATMSystem {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        AccountService accService = new AccountService();
        TransactionService txService = new TransactionService(accService);

        while (true) {
            System.out.println("=== ATM登录界面 ===");
            System.out.println("1. 登录账户");
            System.out.println("2. 查询余额（无需登录）");
            System.out.println("3. 退出系统");
            System.out.print("请输入操作序号：");

            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    System.out.print("请输入卡号：");
                    String accNo = sc.nextLine();
                    System.out.print("请输入密码：");
                    String pwd = sc.nextLine();
                    if (accService.login(accNo, pwd)) {
                        System.out.println("登录成功！");
                        secondMenu(sc, accService, txService);
                    } else {
                        System.out.println("账号或密码错误！");
                    }
                    break;
                case "2":
                    System.out.print("请输入卡号：");
                    String queryAcc = sc.nextLine();
                    System.out.print("请输入密码：");
                    String queryPwd = sc.nextLine();
                    if (accService.login(queryAcc, queryPwd)) {
                        txService.queryBalance();
                        accService.logout();
                    } else {
                        System.out.println("查询失败，账号或密码错误！");
                    }
                    break;
                case "3":
                    System.out.println("退出系统成功！");
                    return;
                default:
                    System.out.println("无效输入！");
            }
        }
    }

    public static void secondMenu(Scanner sc, AccountService accService, TransactionService txService) {
        while (true) {
            System.out.println("=== ATM系统 ===");
            System.out.println("1. 存款");
            System.out.println("2. 取款");
            System.out.println("3. 转账");
            System.out.println("4. 查询余额");
            System.out.println("5. 修改密码");
            System.out.println("6. 退出");
            System.out.println("7.查询交易记录");
            System.out.print("请输入操作序号：");

            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    System.out.print("请输入存款金额：");
                    txService.deposit(Double.parseDouble(sc.nextLine()));
                    break;
                case "2":
                    System.out.print("请输入取款金额：");
                    txService.withdraw(Double.parseDouble(sc.nextLine()));
                    break;
                case "3":
                    System.out.print("请输入目标账户：");
                    String to = sc.nextLine();
                    System.out.print("请输入转账金额：");
                    txService.transfer(to, Double.parseDouble(sc.nextLine()));
                    break;
                case "4":
                    txService.queryBalance();
                    break;
                case "5":
                    System.out.print("请输入旧密码：");
                    String oldPass = sc.nextLine();
                    System.out.print("请输入新密码：");
                    String newPass = sc.nextLine();
                    if (accService.changePassword(oldPass, newPass)) {
                        System.out.println("密码修改成功！");
                    } else {
                        System.out.println("密码修改失败！");
                    }
                    break;
                case "6":
                    accService.logout();
                    return;
                case"7":
                    txService.queryTransactions();
                default:
                    System.out.println("输入无效！");
            }
        }
    }
}